var groupManager_page={};

groupManager_page.action = {
    initBody:function () {
        groupManager_page.interface.initTablePage();
        groupManager_page.action.bindEvent();
    },
    bindEvent:function () {
        $("#addBtn").click(function () {
            groupManager_page.interface.initFormPage();
            return false;
        });
        $("#saveBtn").click(function () {
            groupManager_page.action.saveBtnClick();
            return false;
        });
        $("#cancelBtn").click(function () {
            groupManager_page.action.initBody();
            return false;
        });
        $("#parentName").click(function () {
            groupManager_page.interface.initGroupPage();
            return false;
        });
        // 查看组织树
        $("#viewGroupTreeBtn").click(function () {
            groupManager_page.interface.initGroupTree();
            return false;
        });
    },
    saveBtnClick:function () {
        var params = {
            id:$("#id").val(),
            groupName:$("#groupName").val(),
            comment:$("#comment").val(),
            orderId:$("#orderId").val(),
            parentId:$("#parentId").val(),
            parentName:$("#parentName").val()
        };
        var result = groupManager_page.action.checkData(groupManager_page.data.action, params);
        if (!result.success) {
            layer.alert(result.error, {
                icon: 0,
                skin: 'layer-ext-moon'
            })
        } else {
            var onSuccess = function (resultData) {
                if (resultData.success) {
                    groupManager_page.action.initBody();
                    layer.msg('保存成功！', {icon: 1});
                    // layer.msg("保存成功！");
                } else {
                    if (resultData.error) {
                        layer.msg(resultData.error, {icon: 2});
                    } else {
                        layer.msg("保存失败！", {icon: 2});
                    }
                    return;
                }
            };
            AppComm.ajax.execute({
                'url': AppComm.url.getBaseURL() + '/groupManager/ajax/saveGroup',
                'data': params,
                'success': onSuccess
            });
        }
    },
    // 在做action动作时校验数据data
    checkData:function (action, data) {
        var result = AppComm.data.newResult();

        if (action=='addSubmit') {
            if(data.parentId==""||data.parentId==null||data.parentId.trim()==""){
                result.success = false;
                result.error = "上级组织不能为空。";
                return result;
            }
            if(data.groupName.length>90){
                result.success = false;
                result.error = "组织名称不能超过40个汉字。";
                return result;
            }
            if(data.orderId==""||data.orderId==null||data.orderId.trim()==""){
                result.success = false;
                result.error = "排序号不能为空。";
                return result;
            }
        }
        return result;
    },
    deleteGroup:function (id) {
        var params = {
            id:id
        };
        var onSuccess = function (resultData) {
            if (resultData.success) {
                layer.msg("删除成功！", {icon: 1});
                groupManager_page.data.tableIns.reload();
            }
        };
        AppComm.ajax.execute({
            'url':AppComm.url.getBaseURL()+'/groupManager/ajax/deleteGroup',
            'data':params,
            'success':onSuccess
        });
    }
};
groupManager_page.interface = {
    // 加载表格页
    initTablePage:function () {
        var html = template("groupManagerList_template");
        $(".groupManager_box").html(html);
        layui.use(['table','form'], function(){
            var table = layui.table;
            //执行表格渲染
            var tableIns = table.render({
                height:625,
                url:AppComm.url.getBaseURL()+'/groupManager/ajax/getGroupList',
                elem:"#tb_list",
                page: true,
                limit:14,
                loading:true,
                id:'id',
                cols: [[
                    // {
                    //     field:'groupKey',
                    //     title:'组织关键字',
                    //     align:'left',
                    // },
                    {
                        field:'groupName',
                        title:'组织名称',
                        align:'left',
                        width:200
                    },
                    {
                        field:'grade',
                        title:'层级',
                        align:'left',
                        width:80
                    },
                    {
                        field:'parentName',
                        title:'上级组织',
                        align:'left',
                        width:200
                    },{
                        field:'orderId',
                        title:'排序号',
                        edit: 'text',
                        align:'left',
                        width:100
                    },{
                        field:'comment',
                        title:'说明',
                        align:'left',
                        width:300
                    }, {
                        title:'操作',
                        fixed:'right',
                        width:180,
                        align:'center',
                        toolbar:'#toolbar'
                    }
                ]]
            });

            groupManager_page.data.tableIns = tableIns;

            table.on('tool(tb_list)', function (obj) {
                var rowData = obj.data;
                var layEvent = obj.event;
                if (layEvent==='editBtn') {
                    groupManager_page.interface.initFormPage(rowData.id);
                    groupManager_page.action.bindEvent();
                }
                if (layEvent==='deleteBtn') {
                    layer.confirm('确定要进行删除吗？',{icon: 3, title:'提示'},function(index){
                        groupManager_page.action.deleteGroup(rowData.id);
                        layer.close(index);
                    }, function(index){
                        layer.close(index);
                    });
                }
            });
            table.on('edit(tb_list)', function(obj){
                var param ={
                    id:obj.data.id,
                    orderId:obj.value,
                };
                var onSuccess = function (resultData) {
                    if (resultData.success) {
                        layer.msg("排序号已修改！", {icon: 1});
                        groupManager_page.action.initBody();
                    } else {
                        lay.msg("修改失败！", {icon: 2});
                    }
                };
                AppComm.ajax.execute({
                    'url': AppComm.url.getBaseURL() + '/groupManager/ajax/updateOrderId',
                    'data': param,
                    'success': onSuccess
                });
            });
            // 查询
            var form = layui.form;
            form.render(null, 'searchForm');
            form.on('submit(searchBtn)', function (data) {
                var postData = {
                    groupName: data.field.groupName
                };
                tableIns.reload({
                    page: {curr:1},
                    where: postData
                });
                return false;
            });
        });

    },
    // 初始化表单页
    initFormPage:function (dataId) {
        if (dataId==undefined || ''==dataId.trim()) {
            // 添加
            var html = template("groupManagerForm_template");
            $(".groupManager_box").html(html);

            $("#parentNameInput").css("display","block");
            $("#parentNameShow").css("display","none");

            groupManager_page.action.bindEvent();
            groupManager_page.data.action="addSubmit";
            // layui.use('form', function(){
            //     var form = layui.form;
                // form.verify({
                //     parentId:function (value) {
                //         if (value.length<=0) {
                //             return "上级组织不能为空";
                //         }
                //     },
                //     groupName:function (value) {
                //         if (value.length>90) {
                //             return "组织名称不能超过40个汉字";
                //         }
                //     },
                //     orderId:function(value) {
                //         if (value == "" || value == null) {
                //             return "排序号不能为空";
                //         }
                //     },

                    // groupKey:function(value){
                    //     if((value==""||value==null)){
                    //         return "操作关键字不能为空";
                    //     }
                    //     var flag = 0;
                    //     var params={
                    //         groupKey:value,
                    //     };
                    //     var onSuccess = function (resultData) {
                    //         if (resultData.success) {
                    //             if(resultData.data!=null){
                    //                 flag++;
                    //             }
                    //         }
                    //     };
                    //     AppComm.ajax.execute({
                    //         'url':AppComm.url.getBaseURL()+'/groupManager/ajax/getGroupListByKey',
                    //         'data':params,
                    //         'async':false,
                    //         'success':onSuccess
                    //     });
                    //     if(flag>0){
                    //         return "角色关键字已存在";
                    //     }
                    //
                    // },
                // });

                // form.on('submit(saveBtn)', function(data){
                //     var onSuccess1 = function (resultData) {
                //         if (resultData.success) {
                //             layer.msg("保存成功！");
                //             groupManager_page.action.initBody();
                //         } else {
                //             if (resultData.error) {
                //                 layer.msg(resultData.error);
                //             } else {
                //                 layer.msg("保存失败！");
                //             }
                //             return;
                //         }
                //     };
                //     AppComm.ajax.execute({
                //         'url':AppComm.url.getBaseURL()+'/groupManager/ajax/saveGroup',
                //         'data':data.field,
                //         'success':onSuccess1
                //     });
                //     return false;
                // });
            // });

        } else {
            // 编辑
            layer.load();
            var params = {
                id:dataId
            };
            var onSuccess = function (resultData) {
                layer.closeAll('loading');
                var html = template("groupManagerForm_template", resultData.data);
                $(".groupManager_box").html(html);

                $("#parentNameInput").css("display","none");
                $("#parentNameShow").css("display","block");

                groupManager_page.action.bindEvent();
                groupManager_page.data.action="editSubmit";

                // layui.use('form', function(){
                //     var form = layui.form;
                //     form.verify({
                //     });
                //
                //     form.on('submit(saveBtn)', function(data){
                //         data.field.id = dataId;
                //         var onSuccess = function (resultData) {
                //             if (resultData.success) {
                //                 layer.msg("保存成功！");
                //                 groupManager_page.action.initBody();
                //             } else {
                //                 layer.msg("保存失败！");
                //             }
                //         };
                //         AppComm.ajax.execute({
                //             'url':AppComm.url.getBaseURL()+'/groupManager/ajax/saveGroup',
                //             'data':data.field,
                //             'success':onSuccess
                //         });
                //     });
                // });
            };
            AppComm.ajax.execute({
                'url':AppComm.url.getBaseURL()+'/groupManager/ajax/getGroupById',
                'data':params,
                'success':onSuccess
            });
        }
    },

    //选择组织界面初始化
    initGroupPage: function () {
        var html = template("groupManagerList_group", {});
        var groupLayer = layer.open({
            type: 1,
            skin: 'layui-layer-rim', //加上边框
            area: '720px', //宽高
            offset: '50px',
            content: html
        });
        layui.use(['table', 'form'], function () {
            var table = layui.table;
            //执行表格渲染
            var groupTable = table.render({
                elem: "#group_list",
                url: AppComm.url.getBaseURL()+'/groupManager/ajax/getGroupList',
                page: {
                    layout: ['count', 'prev', 'page', 'next', 'skip']
                },
                limit: 10,
                heigth:"600px",
                loading: true,
                id: 'group_list',
                cols: [[
                    {
                        field: 'groupName',
                        title: '组织名称',
                        width: 150,
                        align: 'left',
                        event: 'setSign',
                    },
                    {
                        field: 'grade',
                        title: '层级',
                        width: 150,
                        align: 'left',
                        event: 'setSign',
                    },
                    {
                        field: 'comment',
                        title: '组织说明',
                        align: 'left',
                        event: 'setSign',
                    }
                ]]
            });

            table.on('tool(group_list)', function (obj) {
                var rowData = obj.data;
                groupManager_page.data.currGroupId = rowData.id;
                $("#parentName").val(rowData.groupName);
                $("#parentId").val(rowData.id);
                // var layEvent = obj.event;
                layer.close(groupLayer);

            });
            // 查询
            var form = layui.form;
            form.render(null, 'searchGroupForm');
            form.on('submit(searchGroup)', function (data) {
                var postData = {
                    groupName: data.field.groupNameInput
                };
                groupTable.reload({
                    page: {curr:1},
                    where: postData
                });
                return false;
            });
            return;
        });
    },
    //初始化查看组织树的页面
    initGroupTree: function () {
        var params = {
            // id: dataId,
        };

        var html = template("groupManagerList_ztree", {});
        var roleAuthLayer = layer.open({
            type: 1,
            area: ['420px', '440px'], //宽高
            btn: ['关闭'], //可以无限个按钮
            yes: function(index, layero){
                layer.close(roleAuthLayer);
            },
            btnAlign: 'c',
            content: html
        });

        var ztree = new CommonZtree('treeDemo', '/groupManager/ajax/getGroupZtree', params);
        ztree.ztreeInit();
    },

};

groupManager_page.data = {
    'tableIns':undefined,
    'currGroupId':'',
    'action':""
};

AppComm.initFrame(groupManager_page);